<?php
/**
 * Created by 上海风车教育科技有限公司.
 * User: 刘红伟
 * Date: 15-11-24
 * Email: 454303753@qq.com
 * File:wechatmenu.php
 */

class wechatmenu{
        private $url;
        private $token;
    public function __construct($token){
        $this->token=$token;
        $this->setUrl();
    }

    public function setUrl(){
      $url="https://api.weixin.qq.com/cgi-bin/menu/create?access_token=".$this->token;
      $this->url=$url;
    }

    public function create($data)
    {


        $http 	= wx_http::factory($this->url, wx_http::TYPE_CURL);
        $json 	= $http->post($this->url, $data, array(
            'User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; InfoPath.1)',
            'Content-Type: application/json; charset=utf-8',
            'Content-Length: '. strlen($data)
        ));
        $result	=json_decode($json,true);

        if ((isset($result['errcode']) ) OR (isset($result['errmsg'])))
        {
            $this->_errno = $result['errcode'];
            $this->_error = $result['errmsg'];

            return FALSE;
        }
        return $result;
    }

    public function setMenu($menuList){
        //树形排布
        $menuList2 = $menuList;
        foreach($menuList as $key=>$menu){
            foreach($menuList2 as $k=>$menu2){
                if($menu['id'] == $menu2['pid']){
                    $menuList[$key]['sub_button'][] = $menu2;
                    unset($menuList[$k]);
                }
            }
        }
        $typeView = 1;
        $typeClick = 2;
        $typescancode_push = 3;
        //处理数据
        foreach($menuList as $key=>$menu){
            //处理type和code
            if($menu['type'] == $typeView){
                $menuList[$key]['type'] = 'view';
                $menuList[$key]['url'] = $menu['code'];
                //处理URL。因为URL不能在转换JSON时被转为UNICODE
                $menuList[$key]['url'] = urlencode($menuList[$key]['url']);
            }else if($menu['type'] == $typeClick){
                $menuList[$key]['type'] = 'click';
                $menuList[$key]['key'] = $menu['code'];
            }else if($menu['type'] == $typescancode_push){
                $menuList[$key]['type'] = 'scancode_push';
                $menuList[$key]['key'] = $menu['code'];
            }
            unset($menuList[$key]['code']);
            //处理PID和ID
            unset($menuList[$key]['id']);
            unset($menuList[$key]['pid']);
            //处理名字。因为汉字不能在转换JSON时被转为UNICODE
            $menuList[$key]['name'] = urlencode($menu['name']);
            //处理子类菜单
            if(isset($menu['sub_button'])){
                unset($menuList[$key]['type']);
                foreach($menu['sub_button'] as $k=>$son){
                    //处理type和code
                    if($son['type'] == $typeView){
                        $menuList[$key]['sub_button'][$k]['type'] = 'view';
                        $menuList[$key]['sub_button'][$k]['url'] = $son['code'];
                        $menuList[$key]['sub_button'][$k]['url'] = urlencode($menuList[$key]['sub_button'][$k]['url']);
                    }else if($son['type'] == $typeClick){
                        $menuList[$key]['sub_button'][$k]['type'] = 'click';
                        $menuList[$key]['sub_button'][$k]['key'] = $son['code'];
                    }else if($son['type'] == $typescancode_push){
                        $menuList[$key]['sub_button'][$k]['type'] = 'scancode_push';
                        $menuList[$key]['sub_button'][$k]['key'] = $son['code'];
                    }
                    unset($menuList[$key]['sub_button'][$k]['code']);
                    //处理PID和ID
                    unset($menuList[$key]['sub_button'][$k]['id']);
                    unset($menuList[$key]['sub_button'][$k]['pid']);
                    //处理名字。因为汉字不能在转换JSON时被转为UNICODE
                    $menuList[$key]['sub_button'][$k]['name'] = urlencode($son['name']);
                }
            }
        }
        //整理格式
        $data = array();
        $menuList = array_values($menuList);
        $data['button'] = $menuList;
        //转换成JSON
        $data = json_encode($data);
        return   $data = urldecode($data);
    }

}